// tslint:disable:member-ordering
import { Component, OnInit, ViewChild } from '@angular/core';
import { HttpService } from '@core/services/http.service';
import swal from 'sweetalert2';

@Component({
    selector: 'app-table-standard',
    templateUrl: './standard.component.html'
})
export class TableStandardComponent implements OnInit {

    data = [];
    ps = 10;
    pi = 1;
    loading = false;
    curRows = [];

    style: any = {
        width: '70%',
        top: '190px'
    };
    style1: any = {
        width: '40%',
        top: '190px'
    };
    allChecked = false;
    indeterminate = false;
    selectedRows = [];
    totalCallNo = 0;
    Total;
    key = '';

    showClass = false;
    tecName;
    tecId;
    majorList = [];
    addMajArr = [];
    glMajarr = [];

    showTec = false;
    selectList = [];
    selectedOption = '';
    classList = [];
    addClaArr = [];
    glClaArr = [];
    majId;

    addTecShow = false;
    teacherName;
    teacherAccount;
    passWord = '123456';
    allMajList = [];
    newMajArr = [];
    newMajName = [];
    allClaList = [];
    newClaName = [];
    newClaArr = [];

    isRemove = false;
    removeTeacName;
    teacTitle;
    sch_id = JSON.parse(localStorage.getItem('_user')).sch_id;

    cloubModal = false;
    cloudList = [];
    cloubTotal;
    cloudArr = [];
    cloubPage = 1;
    tech_id;

    courseModal = false;
    courseList = [];
    courseArr = [];
    courseTotal;


    constructor(public http: HttpService) {
    }


    ngOnInit() {
        this.getData();

    }

    showCloudModal(data) {
        this.cloudArr = [];
        this.cloubModal = true;
        this.tech_id = data.id;
        this.http.getSchoolCloudListBySchId({ sch_id: this.sch_id, pi: this.cloubPage, ps: 9999 }).subscribe(res => {
            // console.log(res)

            this.cloudList = res['list'];
            this.courseTotal = res['total']

            this.http.getCloudListByTechId({ tech_id: this.tech_id }).subscribe(data => {
                // console.log(data)
                for (let zlist of this.cloudList) {
                    zlist['isCheck'] = false
                    for (let ylist of data['list']) {
                        if (zlist.p_id == ylist.cloud_id) {
                            this.cloudArr.push(zlist.p_id);
                            zlist.isCheck = true
                        }
                    }
                }
            })
        })

    }

    showCourseModal(data) {
        this.courseArr = [];
        this.courseModal = true;
        this.tech_id = data.id;
        this.http.getCListBySchId({ sch_id: this.sch_id, pi: this.cloubPage, ps: 9999 }).subscribe(res => {
            // console.log(res)

            this.courseList = res['list'];
            this.cloubTotal = res['total']

            this.http.getCListByTechId({ tech_id: this.tech_id }).subscribe(data => {
                for (let zlist of this.courseList) {
                    zlist['isCheck'] = false;
                    for (let ylist of data['list']) {
                        if (zlist.cour_id == ylist.course_id) {
                            this.courseArr.push(zlist.cour_id);
                            zlist.isCheck = true;
                        }
                    }
                }
                console.log(this.courseList);
            });
        });
    }

    SaveTeacherCloud() {

        this.http.SaveTeacherCloud({ tech_id: this.tech_id, cloudArr: this.cloudArr }).subscribe(res => {
            swal(res['msg'], '', 'info');
            this.cloubModal = false;
            this.getData();
        });
    }

    SaveTeacherCourse() {
        this.http.SaveTeacherCourse({ tea_id: this.tech_id, cArr: this.courseArr }).subscribe(res => {
            swal(res['msg'], '', 'info');
            this.courseModal = false;
            this.getData();
        });
    }

    cloudChange($event, list) {
        if ($event) {
            this.cloudArr.push(list.p_id);
        } else {
            this.removeByValue(this.cloudArr, list.p_id);
        }
    }
    courseChange($event, list) {
        if ($event) {
            this.courseArr.push(list.cour_id);
        } else {
            this.removeByValue(this.courseArr, list.cour_id);
        }
    }

    addTeacher() {
        this.tecId = '';
        this.addTecShow = true;
        this.teacTitle = '添加老师';
        let majparam = {
            key: '',
            pi: '1',
            ps: '9999',
            sch_id: this.sch_id
        };

        this.http.getMajorList(majparam).subscribe(res => {
            this.allMajList = res['list'];
            for (let i = 0; i < this.allMajList.length; i++) {
                this.allMajList[i]['ischeck'] = false;
            }
        });
    }

    saveAddTec() {
        if (!this.teacherName || !this.teacherAccount || !this.passWord) {
            swal('老师姓名、账号为必填项', '', 'info');
            return false;
        }
        let param = {
            UserName: this.teacherName,
            LoginName: this.teacherAccount,
            LoginPwd: '123456',
            majArr: this.newMajArr,
            claArr: this.newClaArr,
            tech_id: this.tecId,
            sch_id: this.sch_id
        };
        this.http.saveTeacher(param).subscribe(res => {
            if (res['code'] == 0) {
                this.getData();
                this.addTecShow = false;
                this.teacherName = '';
                this.teacherAccount = '';
                this.newMajArr = [];
                this.newClaArr = [];
                this.allMajList = [];
                this.newMajArr = [];
                this.newMajName = [];
                this.allClaList = [];
                this.newClaName = [];
                this.newClaArr = [];
            }
            swal(res['msg'], '', 'info');
        });
    }

    clearAddTec() {
        this.addTecShow = false;
        this.teacherName = '';
        this.teacherAccount = '';
        this.newMajArr = [];
        this.newClaArr = [];
        this.allMajList = [];
        this.newMajArr = [];
        this.newMajName = [];
        this.allClaList = [];
        this.newClaName = [];
        this.newClaArr = [];
    }

    newShowClass(i) {
        let majid = this.newMajArr[i];
        let classparam = {
            maj_id: majid
        };

        this.http.getClassByMajId(classparam).subscribe(res => {
            this.allClaList = res['list'];
            for (let i = 0; i < this.allClaList.length; i++) {
                this.allClaList[i]['ischeck'] = false

                for (let j = 0; j < this.newClaArr.length; j++) {
                    if (this.allClaList[i].cla_id == this.newClaArr[j]) {
                        this.allClaList[i].ischeck = true;
                    }
                }
            }
        });
    }

    zyChange($event, major) {

        if ($event.target.checked) {
            this.addMajArr.push(major.maj_Id);
            this.glMajarr.push(major.major_name);
        } else {
            this.removeByValue(this.addMajArr, major.maj_Id);
            this.removeByValue(this.glMajarr, major.major_name);
        }
    }

    zyNewChange($event, major) {

        if ($event.target.checked) {
            this.newMajArr.push(major.maj_Id);
            this.newMajName.push(major.major_name);
        } else {
            this.removeByValue(this.newMajArr, major.maj_Id);
            this.removeByValue(this.newMajName, major.major_name);
            if (!this.newMajArr.length) {
                this.allClaList = [];
                this.newClaArr = [];
                this.newClaName = [];
            }

        }
        //console.log(this.glMajarr);
    }

    clChange($event, cla) {

        if ($event.target.checked) {
            this.addClaArr.push(cla.cla_id);
            this.glClaArr.push(cla.cla_name);
        } else {
            this.removeByValue(this.addClaArr, cla.cla_id);
            this.removeByValue(this.glClaArr, cla.cla_name);
        }
        //  console.log(this.addClaArr);
    }

    clNewChange($event, cla) {

        if ($event.target.checked) {
            this.newClaArr.push(cla.cla_id);
            this.newClaName.push(cla.cla_name);
        } else {
            this.removeByValue(this.newClaArr, cla.cla_id);
            this.removeByValue(this.newClaName, cla.cla_name);
        }
        //  console.log(this.addClaArr);
    }

    saveMajor() {
        let param = {
            tech_id: this.tecId,
            majArr: this.addMajArr
        };
        this.http.addMajTech(param).subscribe(res => {
            // console.log(res);
            this.showClass = false;
            this.addMajArr = [];
            this.glMajarr = [];
            this.getData();
        });
    }

    saveClass() {
        let param = {
            tech_id: this.tecId,
            maj_id: this.majId,
            claArr: this.addClaArr
        };
        this.http.addClassByMajId(param).subscribe(res => {
            // console.log(res);
            this.showTec = false;
            this.addClaArr = [];
            this.glClaArr = [];
            this.getData();
        });
    }

    selectMaj($event) {
        let val = document.querySelector('.ant-select-selection-selected-value');
        val.innerHTML = $event;
        let param = {
            tech_id: this.tecId,
            maj_id: this.majId,
            claArr: this.addClaArr
        };
        this.http.addClassByMajId(param).subscribe(res => {
            // console.log(res);
            this.addClaArr = [];
            this.glClaArr = [];
        });
        for (let i = 0; i < this.selectList.length; i++) {
            let select = this.selectList[i];
            if (select.major_name == $event) {
                this.tecId = select.tea_id;
                this.majId = select.maj_id;

                let classparam = {
                    maj_id: this.majId
                };

                this.http.getClassByMajId(classparam).subscribe(res => {
                    //console.log(res);
                    this.classList = res['list'];
                    for (let i = 0; i < this.classList.length; i++) {
                        this.classList[i]['ischeck'] = false;
                    }

                    let param = {
                        tech_id: this.tecId,
                        maj_id: this.majId
                    };
                    this.http.getClassMaj(param).subscribe(res => {
                        let list = res['list'];
                        //console.log(list);
                        if (list.length) {
                            for (let i = 0; i < list.length; i++) {
                                this.addClaArr.push(list[i].cla_id);
                            }
                            for (let i = 0; i < this.classList.length; i++) {
                                let zyid = this.classList[i].cla_id;
                                for (let j = 0; j < list.length; j++) {
                                    let glid = list[j].cla_id;
                                    if (zyid == glid) {
                                        this.glClaArr.push(this.classList[i].cla_name);
                                        this.classList[i]['ischeck'] = true;
                                    }
                                }
                            }
                        }
                    });
                });
            }
        }
    }

    edit(data) {
        this.addTecShow = true;
        this.teacTitle = '修改';
        this.tecId = data.id;
        let param = {
            tech_id: this.tecId
        };
        this.http.getTeacherInfo(param).subscribe(res => {
            let user = res['user'];
            this.teacherName = user.UserName;
            this.teacherAccount = user.LoginName;
            let majlist = res['MajArr'];
            let clalist = res['ClaArr'];

            let majparam = {
                key: '',
                pi: '1',
                ps: '9999',
                sch_id: this.sch_id
            };

            this.http.getMajorList(majparam).subscribe(res => {
                this.allMajList = res['list'];

                for (let i = 0; i < this.allMajList.length; i++) {
                    this.allMajList[i]['ischeck'] = false;
                    for (let j = 0; j < majlist.length; j++) {
                        if (this.allMajList[i].maj_Id == majlist[j].maj_id) {
                            this.allMajList[i].ischeck = true;
                            this.newMajArr.push(this.allMajList[i].maj_Id);
                            this.newMajName.push(this.allMajList[i].major_name);
                        }
                    }
                }

                if (majlist.length) {
                    for (let i = 0; i < clalist.length; i++) {
                        this.newClaName.push(clalist[i].cla_name);
                        this.newClaArr.push(clalist[i].cla_id);
                    }
                    let majid = majlist[0].maj_id;
                    let classparam = {
                        maj_id: majid
                    };

                    this.http.getClassByMajId(classparam).subscribe(res => {
                        this.allClaList = res['list'];
                        for (let i = 0; i < this.allClaList.length; i++) {
                            this.allClaList[i]['ischeck'] = false;
                            for (let j = 0; j < clalist.length; j++) {
                                if (this.allClaList[i].cla_id == clalist[j].cla_id) {
                                    this.allClaList[i].ischeck = true;
                                }
                            }
                        }
                    });
                }

            });

        });
    }

    isDelTec(list) {
        this.tecId = list.id;
        this.isRemove = true;
        this.removeTeacName = list.UserName;

    }

    removeTec() {
        let param = {
            tech_id: this.tecId
        };
        this.http.removeTeacher(param).subscribe(res => {
            if (res['code'] == 0) {
                this.isRemove = false;
                this.getData();
            }
            swal(res['msg'], '', 'info');
        });
    }

    removeByValue(arr, val) {
        for (var i = 0; i < arr.length; i++) {
            if (arr[i] == val) {
                arr.splice(i, 1);
                break;
            }
        }
    }

    _value;

    doSearch() {
        if (this._value) {
            this.key = this._value;
            this.getData()
        } else {
            this.key = '';
            this.getData();
        }
        // console.log(this._value);
    }

    getData() {
        this.loading = true;
        let param = {
            key: this.key,
            ps: this.ps,
            pi: this.pi,
            sch_id: this.sch_id
        };
        this.http.getTeacherList(param).subscribe(res => {
            this.data = res['list'];
            this.Total = res['total'];
            this.key = '';
            this.loading = false;
        });
    }

    checkAll(value: boolean) {
        this.curRows.forEach(i => {
            if (!i.disabled) i.checked = value;
        });
        this.refreshStatus();
    }

    refreshStatus() {
        const allChecked = this.curRows.every(value => value.disabled || value.checked);
        const allUnChecked = this.curRows.every(value => value.disabled || !value.checked);
        this.allChecked = allChecked;
        this.indeterminate = (!allChecked) && (!allUnChecked);
        this.selectedRows = this.data.filter(value => value.checked);
        this.totalCallNo = this.selectedRows.reduce((total, cv) => total + cv.callNo, 0);
    }

    dataChange(res: any) {

        this.curRows = res;
        this.refreshStatus();
    }

    pageChange(pi: number) {

        this.pi = pi;
        this.loading = true;
        this.getData();
        this.loading = false;
    }

    showMajorModal = (i) => {
        this.showClass = true;
        //console.log(i)
        this.tecId = i.id;
        //console.log('tecid---' + this.tecId);
        this.tecName = i.UserName;
        let majparam = {
            key: '',
            pi: '1',
            ps: '9999',
            sch_id: this.sch_id
        };
        let majlistparam = {
            tech_id: i.id
        };
        this.http.getMajorList(majparam).subscribe(res => {
            this.majorList = res['list'];
            for (let i = 0; i < this.majorList.length; i++) {
                this.majorList[i]['ischeck'] = false;
            }
            //console.log(res);
            this.http.getMajTech(majlistparam).subscribe(res => {
                let list = res['list'];
                //console.log(list)
                if (list.length) {
                    for (let i = 0; i < list.length; i++) {
                        this.addMajArr.push(list[i].maj_id);
                    }
                    for (let i = 0; i < this.majorList.length; i++) {
                        let zyid = this.majorList[i].maj_Id;
                        for (let j = 0; j < list.length; j++) {
                            let glid = list[j].maj_id;
                            if (zyid == glid) {
                                this.glMajarr.push(this.majorList[i].major_name);
                                this.majorList[i]['ischeck'] = true;
                            }
                        }
                    }
                } else {
                    this.glMajarr = [];
                    this.addMajArr = [];
                }
            });
        });


    };

    showTecModal = (i) => {
        // console.log(i);

        if (i.majorTotal == 0) {
            swal('请关联专业', '', 'info');
            return false;
        }
        this.tecId = i.id;
        this.showTec = true;
        let majlistparam = {
            tech_id: i.id
        };

        this.http.getMajTech(majlistparam).subscribe(res => {
            let list = res['list'];
            //console.log(list);
            this.selectList = list;
            this.selectedOption = this.selectList[0].major_name;
            let val = document.querySelector('.ant-select-selection-selected-value');
            val.innerHTML = this.selectList[0].major_name;
            //console.log(this.selectedOption)
            this.majId = this.selectList[0].maj_id;
            let classparam = {
                maj_id: this.majId
            };
            this.http.getClassByMajId(classparam).subscribe(res => {
                // console.log(res);
                this.classList = res['list'];
                for (let i = 0; i < this.classList.length; i++) {
                    this.classList[i]['ischeck'] = false;
                }
                let param = {
                    tech_id: this.tecId,
                    maj_id: this.majId
                };
                this.http.getClassMaj(param).subscribe(res => {
                    let list = res['list'];
                    //   console.log(list);
                    if (list.length) {
                        for (let i = 0; i < list.length; i++) {
                            this.addClaArr.push(list[i].cla_id);
                        }
                        for (let i = 0; i < this.classList.length; i++) {
                            let zyid = this.classList[i].cla_id;
                            for (let j = 0; j < list.length; j++) {
                                let glid = list[j].cla_id;
                                if (zyid == glid) {
                                    this.glClaArr.push(this.classList[i].cla_name);
                                    this.classList[i]['ischeck'] = true;
                                }
                            }
                        }
                    } else {
                        this.addClaArr = [];
                        this.glClaArr = [];
                    }
                });
            });
        });
    };

    clearMaj() {
        this.showClass = false;
        this.glMajarr = [];
        this.addMajArr = [];
    }

    clearCla() {
        this.showTec = false;
        this.addClaArr = [];
        this.glClaArr = [];
    }

}
